﻿/*
 *   Copyright 2011 Peter Crossley
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 *  ----------------------------------------------------------------------------
 *  File:       RunContext.cs
 *  Author:     Peter Crossley
 *  ----------------------------------------------------------------------------
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UEC.Modules.Configuration
{
    public class ServiceContext
    {
        private string _log = "";
        private int indent = 0;
        private CloudManager _manager;

        public ServiceContext(CloudManager manager)
        {
            _manager = manager;
        }

        public void Log(string message)
        {
            if (!string.IsNullOrEmpty(message))
            {
                string tmplog = _GetPrefix() + message;
                _manager.WriteConsole(tmplog, false);
                _log += tmplog + "\r\n";
            }
        }

        private string _GetPrefix()
        {
            string pre = "";
            for (int i = 0; i < indent; i++)
            {
                pre += "\t";
            }
            return pre;
        }

        public void ServiceBegin()
        {
            _manager.WriteConsole("-");
            indent++;
        }
        
        public void ServiceEnd()
        {
            indent--;
            if (indent < 0)
            {
                indent = 0;
            }
        }

        public void FlushContext()
        {
            if (!string.IsNullOrEmpty(_log))
            {
                _manager.LogEvent(_log, System.Diagnostics.EventLogEntryType.Information, true);
            }
            _log = "";
            indent = 0;
        }


    }
}
